/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLThuVien.BLL;

import QLThuVien.DAL.MyConnectUnit;
import QLThuVien.DTO.*;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Rado
 */
public class BDocGia {
    MyConnectUnit connect;
    
    public BDocGia(){
        //this.connect = new MyConnectUnit("localhost", "root", "123456", "QLTV");
        this.connect = new MyConnectUnit("", "sa", "123456", "QLTV");
    }
    
     public HashSet<DocGia> getDocGias(String Condition, String OrderBy) throws Exception{
         ResultSet result = this.connect.Select("DOCGIA", Condition, OrderBy);
         HashSet<DocGia> docgias = new HashSet<DocGia>();
         while(result.next()){
             DocGia docgia = new DocGia();
             docgia.setMaDocGia(result.getString("MaDocGia"));
             docgia.setMaLoaiDocGia(result.getString("MaLoaiDocGia"));
             docgia.setHoTenDocGia(result.getString("HoTenDocGia"));
             docgia.setNgaySinh(result.getString("NgaySinh"));
             docgia.setDiaChi(result.getString("DiaChi"));
             docgia.setEmail(result.getString("Email"));
             docgia.setNgayLapThe(result.getString("NgayLapThe"));
             docgia.setNgayHetHan(result.getString("NgayHetHan"));
             docgia.setTienNo(result.getString("TienNo"));
             docgia.setMatKhau(result.getString("MatKhau"));
             docgia.setSoSachDangMuon(result.getString("SoSachDangMuon"));
             docgias.add(docgia);
         }
         return docgias;
     }
    
    public HashSet<DocGia> getDocGias(String Condition) throws Exception{
        return getDocGias(Condition, null);
    }
    
    public HashSet<DocGia> getDocGias() throws Exception{
        return getDocGias(null);
    }
    
    public DocGia getByID(String id) throws Exception{
        HashSet<DocGia> docgias = this.getDocGias("MaDocGia = " + id);
        if(docgias.size() > 0)
            return docgias.toArray(new DocGia[docgias.size()])[0];
        return null;
    }
    
    public void InsertDocGia(DocGia docgia) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("MaDocGia", docgia.getMaDocGia());       
        map.put("MaLoaiDocGia", docgia.getMaLoaiDocGia());
        map.put("HoTenDocGia", docgia.getHoTenDocGia());       
        map.put("NgaySinh", docgia.getNgaySinh());
        map.put("DiaChi", docgia.getDiaChi());       
        map.put("Email", docgia.getEmail());
        map.put("NgayLapThe", docgia.getNgayLapThe());       
        map.put("NgayHetHan", docgia.getNgayHetHan());
        map.put("TienNo", docgia.getTienNo());       
        map.put("MatKhau", docgia.getMatKhau());
        map.put("SoSachDangMuon", docgia.getSoSachDangMuon()); 
        this.connect.Insert("DOCGIA", map);
    }
    
    public void InsertDocGias(HashSet<DocGia> docgias) throws Exception{
        for(DocGia docgia:docgias)
            this.InsertDocGia(docgia);
    }
    
    public void UpdateDocGia(DocGia docgia) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("MaLoaiDocGia", docgia.getMaLoaiDocGia());
        map.put("HoTenDocGia", docgia.getHoTenDocGia());       
        map.put("NgaySinh", docgia.getNgaySinh());
        map.put("DiaChi", docgia.getDiaChi());       
        map.put("Email", docgia.getEmail());
        map.put("NgayLapThe", docgia.getNgayLapThe());       
        map.put("NgayHetHan", docgia.getNgayHetHan());
        map.put("TienNo", docgia.getTienNo());       
        map.put("MatKhau", docgia.getMatKhau());
        map.put("SoSachDangMuon", docgia.getSoSachDangMuon()); 
        this.connect.Update("DOCGIA", map, "MaDocGia = " + docgia.getMaDocGia());
    }
    
    public void UpdateDocGias(HashSet<DocGia> docgias) throws Exception{
        for(DocGia docgia:docgias)
            this.UpdateDocGia(docgia);
    }
    
    public void DeleteDocGia(DocGia docgia) throws Exception{
        this.connect.Delete("DOCGIA", "MaDocGia = " + docgia.getMaDocGia());
    }
    
    public void DeleteDocGias(HashSet<DocGia> docgias) throws Exception{
        for(DocGia docgia:docgias)
            this.DeleteDocGia(docgia);
    }
}
